package avicit.bdp.dgs.metadata.util;

import avicit.bdp.core.constant.Constants;
import avicit.bdp.core.constant.ProcessConstants;
import avicit.bdp.core.enums.MetadataType;
import avicit.bdp.core.util.json.JSONUtils;
import avicit.bdp.dds.dispatch.enums.TaskType;
import avicit.bdp.dds.dispatch.model.ProcessData;
import avicit.bdp.dds.dispatch.model.TaskNode;
import avicit.bdp.dds.dispatch.process.Property;
import avicit.bdp.dds.dispatch.task.collect.MetadataCollectParameters;
import avicit.bdp.dds.dispatch.task.conditions.ConditionsParameters;
import avicit.bdp.dgs.metadata.dto.MetadataCollectTaskDTO;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author mayi 元数据采集任务构建工具类
 */
public class MetaDataProcessDefinitionUtil {

  public static Map<String, Object> generateProcessDefinition(MetadataCollectTaskDTO taskDTO) {
    Map<String, Object> definitionMap = new HashMap<>(16);
    definitionMap.put(ProcessConstants.ID, taskDTO.getId());
    definitionMap.put(ProcessConstants.PROJECT_ID, 123456);
    definitionMap.put(ProcessConstants.TYPE, ProcessConstants.JOB_METADATA_COLLECT);
    definitionMap.put(ProcessConstants.CONNECTS, "");
    definitionMap.put(ProcessConstants.LOCATIONS, "");
    definitionMap.put(ProcessConstants.NAME, taskDTO.getName());
    definitionMap.put(ProcessConstants.REMARK, taskDTO.getRemark());
    definitionMap.put(ProcessConstants.RELEASE_STATE, 0);
    definitionMap.put(
        ProcessConstants.PROCESS_DEFINITION_JSON,
        JSONUtils.toJsonString(generateProcessData(taskDTO)));
    definitionMap.put(ProcessConstants.EXTRA_PARAMS, "");
    return definitionMap;
  }

  private static ProcessData generateProcessData(MetadataCollectTaskDTO taskDTO) {
    ProcessData processData = new ProcessData();
    processData.setTasks(generateTaskNode(taskDTO));
    return processData;
  }

  private static List<TaskNode> generateTaskNode(MetadataCollectTaskDTO taskDTO) {

    List<TaskNode> taskNodeList = new LinkedList<>();
    TaskNode taskNode = new TaskNode();
    taskNode.setId(ProcessConstants.TASKS + Constants.SUBTRACT_STRING + 1);
    taskNode.setType(TaskType.METADATA_COLLECT.name());
    taskNode.setName(taskDTO.getName());
    taskNode.setDesc(taskDTO.getRemark());
    taskNode.setConditionResult(JSONUtils.toJsonString(new ConditionsParameters()));

    MetadataCollectParameters parameters = new MetadataCollectParameters();
    parameters.setDataSourceId(taskDTO.getDatasourceId());
    parameters.setAllDatabase(taskDTO.getAllDatabase() != null && taskDTO.getAllDatabase() == 1);
    parameters.setTables(taskDTO.getTables());
    parameters.setMetadataCollectTaskId(taskDTO.getId());
    // parameters.setMetadataType(taskDTO.getMetadataType());
    parameters.setMetadataType(MetadataType.TABLE.ordinal());

    List<Property> localParams = new ArrayList<>();
    parameters.setLocalParams(localParams);

    ConditionsParameters conditionsParameters = new ConditionsParameters();
    taskNode.setConditionResult(JSONUtils.toJsonString(conditionsParameters));
    taskNode.setParams(JSONUtils.toJsonString(parameters));
    taskNodeList.add(taskNode);
    return taskNodeList;
  }
}
